package com.qf.dao.impl;

import com.qf.dao.UserDao;
import com.qf.pojo.Admin;
import com.qf.pojo.User;
import com.qf.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author:zhangyang
 * @Date: 11:59
 * @Version 1.0
 * //访问的实现层
 */
public class UserDaoImpl implements UserDao {

    //创建QueryRunner对象
    private QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()) ;
    @Override
    public List<User> selectUserList() {

        //查询
        try {
        //sql
            String sql = "select * from user" ;

            return   qr.query(sql,new BeanListHandler<User>(User.class)) ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //管理员登录
    @Override
    public Admin selectAdmin(Admin admin) {
        String sql = "select *  from admin where username = ? and password = ?" ;
        try {
          return   qr.query(sql,new BeanHandler<Admin>(Admin.class),admin.getUsername(),admin.getPassword()) ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 添加客户信息
     * @param user
     */
    @Override
    public void add(User user) {
        String sql = "insert into user values(null,?,?,?,?,?,?)" ;
        //更新
        try {
            int count = qr.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据id删除用户
     * @param id
     */
    @Override
    public void delUser(int id) {
        String sql = "delete from user where id = ?" ;
        try {
            int count = qr.update(sql, id);
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过编号查询用户信息
     * @param id
     * @return
     */
    @Override
    public User selectUserById(int id) {
        String sql = "select * from user where id = ?" ;
        try {
            User user = qr.query(sql,new BeanHandler<User>(User.class),id) ;
            return user ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    //更新
    @Override
    public void update(User user) {
        String sql = "update user set name = ?,gender = ? ,age = ? ,address=?,qq=?,email=? where id = ?" ;
        try {
           int count =  qr.update(sql,user.getName(),user.getGender(),user.getAge(),user.getAddress(),user.getQq(),user.getEmail(),user.getId()) ;
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 查询总记录数数
     * @return
     */
    @Override
    public int getTotalCount() {
        //sql
        String sql = "select count(*) from user" ; //总记录
        //查询
        try {
            Object object = qr.query(sql, new ScalarHandler<>()); //Integer
            int count = Integer.parseInt(String.valueOf(object));
            return count ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    //查询用户的分页列表数据
    @Override
    public List<User> findUserByPageList(int start, int pageSize) {
        String sql = "select * from user limit ?,?" ;
        //执行查询
        try {
            List<User> list = qr.query(sql, new BeanListHandler<User>(User.class), start, pageSize);
            return list ;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
